package com.leyou.order.config;

import com.leyou.common.auth.utils.RsaUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.security.PublicKey;

/**
 * @package com.leyou.gateway.config
 * @description: 网关token配置类
 * @author: 许超越
 * @date 2019/7/9 16:16
 * @version: V1.0
 */
@Slf4j
@Data
@ConfigurationProperties(prefix = "ly.jwt")
public class JwtProperties implements InitializingBean {

    /**
     * 公钥地址
     */
    private String pubKeyPath;

    /**
     * 公钥
     */
    private PublicKey publicKey;

    /**
     * 服务token属性信息
     */
    private AppTokenProperties appTokenProperties = new AppTokenProperties();


    @Data
    public class AppTokenProperties {
        /**
         * 服务的id
         */
        private Long id;
        /**
         * 服务的密码
         */
        private String secret;
        /**
         * 存放token认证信息的请求头的名称
         */
        private String headerName;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        try {
            this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
        } catch (Exception e) {
            log.error("读取公钥失败，原因：{}", e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }
}
